/*     */ package com.zimbra.cs.dav.resource;
/*     */ 
/*     */ import com.google.common.io.Closeables;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.index.MessageHit;
/*     */ import com.zimbra.cs.index.SortBy;
/*     */ import com.zimbra.cs.index.ZimbraHit;
/*     */ import com.zimbra.cs.index.ZimbraQueryResults;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxIndex;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mime.MPartInfo;
/*     */ import com.zimbra.cs.mime.Mime;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.InputStream;
/*     */ import java.util.List;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.internet.MimePart;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class Attachment
/*     */   extends PhantomResource
/*     */ {
/*     */   private byte[] mContent;
/*     */   
/*     */   public Attachment(String uri, String owner, long date, int contentLength)
/*     */   {
/*  62 */     this(uri, owner, parseUri(uri));
/*  63 */     setCreationDate(date);
/*  64 */     setLastModifiedDate(date);
/*  65 */     setProperty("getcontentlength", Integer.toString(contentLength));
/*     */   }
/*     */   
/*     */   public Attachment(String uri, String owner, List<String> tokens, DavContext ctxt) throws DavException {
/*  69 */     this(uri, owner, tokens);
/*  70 */     String user = ctxt.getUser();
/*  71 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*  73 */     String name = (String)tokens.get(tokens.size() - 1);
/*  74 */     StringBuilder query = new StringBuilder();
/*  75 */     boolean needQuotes = name.indexOf(' ') > 0;
/*     */     
/*     */ 
/*     */ 
/*  79 */     query.append("filename:");
/*  80 */     if (needQuotes)
/*  81 */       query.append("'");
/*  82 */     query.append(name);
/*  83 */     if (needQuotes)
/*  84 */       query.append("'");
/*  85 */     ZimbraQueryResults zqr = null;
/*  86 */     boolean found = false;
/*     */     try {
/*  88 */       Account account = prov.get(Key.AccountBy.name, user);
/*  89 */       Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
/*     */       
/*  91 */       zqr = mbox.index.search(ctxt.getOperationContext(), query.toString(), SEARCH_TYPES, SortBy.NAME_ASC, 10);
/*  92 */       if (zqr.hasNext()) {
/*  93 */         ZimbraHit hit = zqr.getNext();
/*  94 */         if ((hit instanceof MessageHit)) {
/*  95 */           Message message = ((MessageHit)hit).getMessage();
/*  96 */           setCreationDate(message.getDate());
/*  97 */           setLastModifiedDate(message.getChangeDate());
/*  98 */           msg = message.getMimeMessage();
/*  99 */           List<MPartInfo> parts = Mime.getParts(msg);
/* 100 */           for (MPartInfo p : parts) {
/* 101 */             String fname = p.getFilename();
/* 102 */             if (name.equals(fname)) {
/* 103 */               String partName = p.getPartName();
/* 104 */               this.mContent = ByteUtil.getContent(Mime.getMimePart(msg, partName).getInputStream(), 0);
/* 105 */               setProperty("getcontentlength", Integer.toString(this.mContent.length));
/* 106 */               setProperty("getcontenttype", p.getContentType());
/* 107 */               found = true;
/* 108 */               break;
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     } catch (Exception e) { MimeMessage msg;
/* 114 */       ZimbraLog.dav.error("can't search for: attachment=" + name, e);
/*     */     } finally {
/* 116 */       Closeables.closeQuietly(zqr);
/*     */     }
/* 118 */     if (!found) {
/* 119 */       throw new DavException("invalid uri", 404, null);
/*     */     }
/*     */   }
/*     */   
/*     */   public Attachment(String uri, String owner, List<String> tokens) {
/* 124 */     super(uri, owner, tokens);
/*     */   }
/*     */   
/*     */   public InputStream getContent(DavContext ctxt)
/*     */   {
/* 129 */     return new ByteArrayInputStream(this.mContent);
/*     */   }
/*     */   
/*     */   public boolean isCollection()
/*     */   {
/* 134 */     return false;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/resource/Attachment.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */